/*
 * 文件名：DeleteDeviceController.java
 * 版权：Copyright by www.tiansu-china.com/
 * 描述：
 * 修改人：congriran
 * 修改时间：2021年2月1日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
*/

package com.tiansu.reborn.wanda.modules.executor.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiansu.reborn.wanda.modules.executor.entity.FacadeResponse;
import com.tiansu.reborn.wanda.modules.executor.entity.vo.DeleteDeviceCondVO;
import com.tiansu.reborn.wanda.modules.executor.service.DeleteDeviceService;

import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
public class DeleteDeviceController
{
    @Autowired
    private DeleteDeviceService deleteDeviceService;

    @GetMapping("/delete_device")
    public String index()
    {
        return "delete_device";
    }

    @ResponseBody
    @PostMapping("/deleteDevice")
    public FacadeResponse deleteDevice(@RequestBody DeleteDeviceCondVO vo, HttpSession session)
    {
        FacadeResponse response = null;
        try
        {
            if (checkVerify(vo.getSecret(), session))
            {
                int num = deleteDeviceService.deleteDevice(vo.getDeviceId(), vo.getDeviceNo());
                response = FacadeResponse.ok("删除了" + num + "条数据！");
            }
            else
            {
                response = FacadeResponse.ok("验证码校验失败");
            }
            
        }
        catch (Exception e)
        {
            response = FacadeResponse.error(e.getMessage());
            log.error("DeleteDevice error, error message is: " + e.getMessage());
        }
        return response;
    }
    
    private boolean checkVerify(String verifyInput, HttpSession session)
    {
        try
        {
            // 从session中获取随机数
            String inputStr = verifyInput;
            String random = (String) session.getAttribute("RANDOMVALIDATECODEKEY");
            if (random == null)
            {
                return false;
            }
            if (random.equals(inputStr))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception e)
        {
            log.error("验证码校验失败", e);
            return false;
        }
    }
}
